package com.soo.hoo.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.soo.hoo.admin.mapper.SysLogMapper;
import com.soo.hoo.admin.model.entity.SysLog;
import com.soo.hoo.admin.service.LogService;
import com.soo.hoo.core.event.OperateLogEvent;
import com.soo.hoo.core.model.MatchItem;
import com.soo.hoo.core.model.Page;
import com.soo.hoo.core.model.Result;
import com.soo.hoo.core.service.impl.ServiceImpl;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>日志管理</p>
 *
 * @author 杨燚
 * @since 2024/1/10
 */
@Service
public class LogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements LogService {

    @Override
    public Page<SysLog> page(Page<SysLog> pagination, Integer businessType) {
        MatchItem matchItem = new MatchItem();
        matchItem.setColumn("businessType");
        matchItem.setValue(businessType);
        matchItem.setAnd(true);
        if (CollUtil.isEmpty(pagination.getMatchItems())) {
            pagination.setMatchItems(new ArrayList<>());
        }
        pagination.getMatchItems().add(matchItem);
        return this.page(pagination);
    }


    @Async
    @Override
    @EventListener
    public void recordOperateLog(OperateLogEvent operateLogEvent) {
        SysLog sysLog = new SysLog();
        BeanUtil.copyProperties(operateLogEvent,sysLog);
        baseMapper.insert(sysLog);
    }
}
